<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>API-TEST</title>
</head>
<body>
	<br>
	<p>
		url: <input id="url" type="text" style="height:30px;width:400px;"/>
		ContentType: 
		<select id="ContentType">
		  <option value ="application/json;charset=utf-8">application/json;charset=utf-8</option>
		  <option value ="application/x-www-form-urlencoded">application/x-www-form-urlencoded</option>
		</select>
		type：
		<select id="type">
		  <option value ="post">post</option>
		  <option value ="get">get</option>
		</select>
	</p>
	<p>
		authorization:
		<select id="authorization">
		  <option value ="1">认证</option>
		  <option value ="2">不认证</option>
		</select>
		<button type="button" id="submit">提交</button>
		<!-- <button type="button" id="submit2">form提交</button> -->
	</p>
	<p>param:</p>
	<p>
		<textarea id="param" rows="15" cols="100"></textarea>
	</p>
	<p>response:</p>
	<p>
		<textarea id="response" rows="30" cols="100"></textarea>
	</p>
	
	<form action="test/tu2" method="post" >
		id:<input name="id"/>
		name:<input name="name"/>
		<button type="submit">提交</button>
	</form>
	<%-- <script type="text/javascript" src="${ctx}/static/jquery.js"></script> --%>
	<script src="http://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script>
	
	<script src="js/aes.js"></script>
	<script>
		
		//var key = CryptoJS.enc.Utf8.parse("ABCDEF0123456789");
		
		/* var key = "ABCDEF0123456789";
		function Encrypt(word){
            var encode =CryptoJS.AES.encrypt(word,key
            ).toString();
            
            var encryptedBase64Str = CryptoJS.enc.Base64.stringify(encode);
        }

        function Decrypt(word){
            return CryptoJS.AES.decrypt(word,pwd).toString(CryptoJS.enc.Utf8);
        } */
        
        var key = "ABCDEF0123456789";
      //AES 加密
        function encryptByAES(message, key) {
            var keyHex = CryptoJS.enc.Utf8.parse(key);
            var encrypted = CryptoJS.AES.encrypt(message, keyHex, {
                mode: CryptoJS.mode.ECB,
                padding: CryptoJS.pad.Pkcs7
            });
            return encrypted.toString();
        }
        //AES 解密
        function decryptByAES(ciphertext, key) {
            var keyHex = CryptoJS.enc.Utf8.parse(key);
            // direct decrypt ciphertext
            var decrypted = CryptoJS.AES.decrypt({
                ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
            }, keyHex, {
                mode: CryptoJS.mode.ECB,
                padding: CryptoJS.pad.Pkcs7
            });
            return decrypted.toString(CryptoJS.enc.Utf8);
        }
			
	 
    
		$("#submit").on("click",function(){
			var base= "http://localhost:9090/FirstMaven";
			var url = base+"/"+$("#url").val();
			var ContentType = $("#ContentType").val();
			var type = $("#type").val();
			var param = $("#param").val();
			var authorization =  $("#authorization").val();
			
			var reqData = param;
			reqData = encryptByAES(param,key);
			
			var reqOpt ={
		        url: url,
		        type: type,
		        contentType : ContentType, 
		        data: reqData,
		        success: function (json) {
		        	var retData =json.data;
		        	retData= decryptByAES(retData,key);
		        	var obj = JSON.parse(retData);
		            $("#response").val(JSON.stringify(obj,null,2));
		        },
		        error:function(XMLHttpRequest, textStatus, errorThrown){
		            console.log(XMLHttpRequest);
		            console.log(textStatus);
		            console.log(errorThrown);
		         }
		    }
			/* if(authorization == "1"){
				reqOpt.beforeSend =function (XMLHttpRequest) {
		        	XMLHttpRequest.setRequestHeader("Authorization", token);
		        	XMLHttpRequest.setRequestHeader("App-Type", "android");
		        	XMLHttpRequest.setRequestHeader("App-Version", "1.0.0");
		        };
			} */
			$.ajax(reqOpt);
		});
		
		
	</script>
</body>
</html>